package com.enzoism.springboot.dive.bis.collection;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 进行对象遍历
 * 1）不要在foreach中进行remove/add,使用Iterator,如果是并发操作需要加锁
 * 2）使用entrySet遍历Map类集合KV，而不是KeySet方式进行遍历
 */
public class MapTest {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        map.put("key4","value4");

        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            System.out.println("----key:"+next.getKey()+" value:"+next.getValue());
        }

    }
}
